package com.ruoyi.vstac.service;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.vstac.config.StartupConfig;
import com.ruoyi.vstac.domain.Deadline;
import com.ruoyi.vstac.mapper.DeadlineMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeoutException;

@Service
public class DeadlineService {
    @Resource
    private DeadlineMapper deadlineMapper;
    @Resource
    private RedisCache redisCache;

    public Deadline getDeadline(){
        return deadlineMapper.selectOne();
    }

    public void setDeadlineToRedis(Deadline deadline){
        // 截止时间
        Date updatedDeadline = deadline.getUploadDeadline();
        Date reviewDeadline = deadline.getReviewDeadline();

        // 将截止时间存到redis
        redisCache.setCacheObject(StartupConfig.UPDATE_DEADLINE,updatedDeadline);
        redisCache.setCacheObject(StartupConfig.REVIEW_DEADLINE,reviewDeadline);
    }

    /**
     * 判断是否到了上传截至时间
     * @throws TimeoutException
     */
    public void isUploadTime() throws TimeoutException {
        Date updatedDeadline = (Date) redisCache.getCacheObject(StartupConfig.UPDATE_DEADLINE);
        Date now = new Date();
        if(now.after(updatedDeadline)){
            throw new TimeoutException("上传作品截止时间已过");
        }
    }

    /**
     * 判断是否到了审查截至时间
     * @throws TimeoutException
     */
    public void isReviewTime() throws TimeoutException {
        Date reviewDeadline = (Date) redisCache.getCacheObject(StartupConfig.REVIEW_DEADLINE);
        Date now = new Date();
        if(now.after(reviewDeadline)){
            throw new TimeoutException("审查作品截止时间已过");
        }
    }

}
